package com.eci.context.util;

import com.eci.common.asset.constant.StringConstant;
import com.eci.common.asset.kit.JwtUserInfo;
import com.eci.common.util.JacksonUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class RequestsUtil {

	private static final String UNKNOWN = "unknown";
	private static final String HEADER_X_FORWARDED_FOR = "x-forwarded-for";
	private static final String HEADER_PROXY_CLIENT_IP = "Proxy-Client-IP";
	private static final String HEADER_WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
	public static final String HEADER_HTTP_CLIENT_IP = "HTTP_CLIENT_IP";
	private static final String HEADER_HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR";

	/**
	 * 	获取当前请求
	 * */
	public static  HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
	}

	/**
	 * 	获取当前会话
	 * */
	public static HttpSession  getSession() {
		return getRequest().getSession();
	}
	
	/**
	 * 	从Token中获取当前用户的部分信息 
	 * */
	public static JwtUserInfo getUser() {
		HttpServletRequest req = getRequest();
		String currentUser = req.getHeader(StringConstant.Authority.AUTH_USER);
		String identityId = req.getHeader(StringConstant.Authority.AUTH_IDENTITY_ID);
		if(currentUser == null)  
			return JwtUserInfo.unknownUser();
		JwtUserInfo user = JacksonUtil.readJson(new String(Base64.decodeBase64(currentUser)), JwtUserInfo.class);
		if(identityId!=null && !identityId.trim().isEmpty()) {
			user.setCompanyId(Integer.parseInt(identityId.trim()));
		}
		return user; 
	}

	/**
	 * 	从Token中获取当前用户的部分信息
	 * */
	public static JwtUserInfo getUser(HttpServletRequest req) {
		String currentUser = req.getHeader(StringConstant.Authority.AUTH_USER);
		String identityId = req.getHeader(StringConstant.Authority.AUTH_IDENTITY_ID);
		if(currentUser == null)
			return JwtUserInfo.unknownUser();
		JwtUserInfo user = JacksonUtil.readJson(new String(Base64.decodeBase64(currentUser)), JwtUserInfo.class);
		if(identityId!=null && !identityId.trim().isEmpty()) {
			user.setCompanyId(Integer.parseInt(identityId.trim()));
		}
		return user;
	}

	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader(HEADER_X_FORWARDED_FOR);
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeader(HEADER_PROXY_CLIENT_IP);
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeader(HEADER_WL_PROXY_CLIENT_IP);
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeader(HEADER_HTTP_CLIENT_IP);
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeader(HEADER_HTTP_X_FORWARDED_FOR);
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
